package com.sad.jetpack.v1.datamodel.demo.testFuture;

import android.content.Context;

import com.sad.jetpack.v1.datamodel.api.IDataModelObtainedCallback;
import com.sad.jetpack.v1.datamodel.api.IDataModelObtainedExceptionListener;
import com.sad.jetpack.v1.datamodel.api.IDataModelProduceFutureObserver;
import com.sad.jetpack.v1.datamodel.api.IDataModelRequest;
import com.sad.jetpack.v1.datamodel.api.IDataModelResponse;
import com.sad.jetpack.v1.datamodel.api.utils.LogcatUtils;

import org.json.JSONObject;

public class TestFutureMain {
    public static void testGo(Context context){
        try {
            JSONObject params=new JSONObject();
            params.put("areaCode","52");
            DJHttpClient.newInstance(context)
                    .method(IDataModelRequest.Method.GET)
                    .url("http://services.gzstv.com/qxj/api/v1/weather/qxj/qryWeatherInfo")
                    .params(params.toString())
                    .log(true)
                    .dataModelProducer("weather_detail")
                    .addInputInterceptor(new DJXGInputInterceptor(),0)
                    .exceptionListener(new IDataModelObtainedExceptionListener() {
                        @Override
                        public void onDataObtainedException(IDataModelRequest request, Throwable throwable) {
                            LogcatUtils.e("-------------->weather_detail异常:"+throwable.getMessage());
                            throwable.printStackTrace();
                        }
                    })
                    .callback(new IDataModelObtainedCallback<String>() {

                        @Override
                        public void onDataObtainedCompleted(IDataModelResponse<String> response) {
                            LogcatUtils.e("-------------->weather_detail回调:"+response.body());
                        }
                    })
                    .execute()
                    .addObserver(true, new IDataModelProduceFutureObserver<String>() {
                        @Override
                        public String observerId() {
                            return "xxxxxx";
                        }

                        @Override
                        public void onDataResponseChanged(IDataModelResponse<String> response) {
                            LogcatUtils.e("-------------->weather_detail全局监听:"+response.body());
                        }
                    });
            ;
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
